package com.tianji.learning.task;

import com.tianji.learning.entity.PointsBoardSeason;
import com.tianji.learning.service.IPointsBoardSeasonService;
import com.tianji.learning.service.IPointsBoardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Slf4j
@Component
public class CreateHistoryPointRecordTableTask {

    @Autowired
   private IPointsBoardSeasonService seasonService;

    @Autowired
    private IPointsBoardService boardService;

    // 秒 分 时 天 月 周
    // 周和天时互斥
    // 月初1号凌晨01：00：00执行
    @Scheduled(cron = "0 0 1 1 * ?")
    public void createHistoryPointRecordTable(){
        // 当前时间的上个月时间
        LocalDateTime prevMonth = LocalDateTime.now().minusMonths(1);
        // 获取上个月的赛季
        PointsBoardSeason season = seasonService.getByDate(prevMonth);
        if(season == null){
            log.error("查询不到赛季");
            return;
        }
        // 创建历史赛季表：points_record_赛季ID
        String tableName  = "points_board_" + season.getId();
        boardService.createTable(tableName);
    }

}
